﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Adams.Admin.DataAccess.Membership
{
    public static class CreateMember
    {
        public static Adams.Admin.DataTransfer.Membership.CreateMember.PageLoad PageLoad(int id)
        {
            var data = new Adams.Admin.DataTransfer.Membership.CreateMember.PageLoad();
            data.Member = new Adams.Shared.Entities.Membership.Member();

            using (SqlDataReader dr = DB.RunDataReader(@"
IF(@MemberID > 0)
BEGIN
    SELECT * FROM Membership.Members WHERE MemberID = @MemberID

    SELECT UA.ApplicationID
    FROM Membership.Members U 
    INNER JOIN Membership.MembersApplications UA ON U.MemberID = UA.MemberID
    WHERE U.MemberID = @MemberID
END
", CommandType.Text,
            DB.CreateParameter("@MemberID", id, SqlDbType.Int)))
            {
                if (id > 0)
                {
                    dr.Read();
                    data.Member.ID = dr["MemberID"].DbInt();
                    data.Member.FirstName = dr["FirstName"].DbString();
                    data.Member.LastName = dr["LastName"].DbString();
                    data.Member.Email = dr["Email"].DbString();
                    data.Member.Street = dr["Street"].DbString();
                    data.Member.Street2 = dr["Street2"].DbString();
                    data.Member.City = dr["City"].DbString();
                    data.Member.State = dr["State"].DbString();
                    data.Member.ZipCode = dr["ZipCode"].DbString();
                    data.Member.Phone = dr["Phone"].DbString();
                    data.Member.IsEmailConfirmed = dr["IsEmailConfirmed"].DbBool();
                    data.Member.IsActive = dr["IsActive"].DbBool();
                    data.Member.IsLocked = dr["IsLocked"].DbBool();
                }
            }

            return data;
        }

        public static void SaveMember(DataTransfer.Membership.CreateMember.SaveMember input)
        {
            DB.Run(@"
IF(@MemberID > 0)
BEGIN
    UPDATE Membership.Members SET
        FirstName = @FirstName, 
        LastName = @LastName,
        Email = @Email,
        Street = @Street,
        Street2 = @Street2,
        City = @City,
        State = @State,
        ZipCode = @ZipCode,
        Phone = @Phone,
        IsActive = @IsActive,
        IsLocked = @IsLocked,
        PasswordHash = @PasswordHash,
        PasswordSalt = @PasswordSalt
    WHERE MemberID = @MemberID
END
ELSE
BEGIN
    INSERT INTO Membership.Members (
        FirstName
        ,LastName
        ,Email
        ,Street
        ,Street2
        ,City
        ,State
        ,ZipCode
        ,Phone
        ,IsActive
        ,IsLocked
        ,PasswordHash
        ,PasswordSalt)
    VALUES (
        @FirstName
        ,@LastName
        ,@Email
        ,@Street
        ,@Street2
        ,@City
        ,@State
        ,@ZipCode
        ,@Phone
        ,@IsActive
        ,@IsLocked
        ,@PasswordHash
        ,@PasswordSalt)

    SET @MemberID = SCOPE_IDENTITY()
END
", CommandType.Text,
                    DB.CreateParameter("@MemberID", input.Member.ID),
                    DB.CreateParameter("@FirstName", input.Member.FirstName.ToNString()),
                    DB.CreateParameter("@LastName", input.Member.LastName.ToNString()),
                    DB.CreateParameter("@Email", input.Member.Email.ToNString()),
                    DB.CreateParameter("@Street", input.Member.Street.ToNString()),
                    DB.CreateParameter("@Street2", input.Member.Street2.ToNString()),
                    DB.CreateParameter("@City", input.Member.City.ToNString()),
                    DB.CreateParameter("@State", input.Member.State.ToNString()),
                    DB.CreateParameter("@ZipCode", input.Member.ZipCode.ToNString()),
                    DB.CreateParameter("@Phone", input.Member.Phone.ToNString()),
                    DB.CreateParameter("@IsActive", input.Member.IsActive),
                    DB.CreateParameter("@IsLocked", input.Member.IsLocked),
                    DB.CreateParameter("@PasswordHash", input.PasswordHash),
                    DB.CreateParameter("@PasswordSalt", input.PasswordSalt));
        }

        public static Adams.Admin.DataTransfer.Membership.CreateMember.MemberInfo GetMemberInfo(int memberId, string email)
        {
            var data = new Adams.Admin.DataTransfer.Membership.CreateMember.MemberInfo();

            using (SqlDataReader dr = DB.RunDataReader(@"
IF (@MemberID > 0)
BEGIN
    SELECT *
    FROM Membership.Members
    WHERE MemberID = @MemberID
END

SELECT COUNT(*) AS Count
FROM Membership.Members
WHERE Email = @Email
", CommandType.Text,
                DB.CreateParameter("@MemberID", memberId),
                DB.CreateParameter("@Email", email)))
            {
                if (memberId > 0)
                {
                    dr.Read();
                    data.MemberID = dr["MemberID"].DbInt();
                    data.Email = dr["Email"].DbString();
                    data.PasswordHash = dr["PasswordHash"].DbString();
                    data.PasswordSalt = dr["PasswordSalt"].DbString();
                    dr.NextResult();
                }
                dr.Read();
                data.Exists = dr["Count"].DbInt() > 0;
            }

            return data;
        }
    }
}
